<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<style>
#addresourcedialog .opercationdiv {
	margin-top: 60px;
	margin-left: 10px;
}

#opercationdiv input {
	width: 150px;
}
</style>
<!-- 受控资源数据表 -->
<div class="resourcetablediv">
	<table id="resourcetable"></table>
</div>
<div id="addresourcedialog">
	<div class="opercationdiv">
		<label>资源名称</label>&nbsp;<input id="name"><br /> <label>类&nbsp;&nbsp;型</label>&nbsp;<input
			id="type"><br /> <label>资源&nbsp;Url</label>&nbsp;<input
			id="url"><br /> <label>父级资源</label>&nbsp;<input
			id="parentname"> <label>顺&nbsp;&nbsp;序</label>&nbsp;<input
			id="ordernum"><input id="id" type="hidden">
	</div>
</div>
<script>
	var id;
	var addOrUpdate = true;
	$(document).ready(function() {
		//初始化操作窗口
		initAddResourceDialog();
		//初始化受控资源表数据
		initResourcTableData();
	})
	//初始化受控资源表数据
	function initResourcTableData() {
		$('#resourcetable').treegrid({
			title : '资源列表',
			nowrap : true,
			striped : true,
			width : 870,
			idField : "id",
			treeField : "name",
			animate : true,
			pagination : true,
			rownumbers : true,
			onBeforeExpand : function(row) {
				loadChildrens(row);
			},
			url : basePath + 'system/getResourceByPageAndSize',
			toolbar : [ {
				text : '增加',
				iconCls : 'icon-add',
				handler : function() {
					addOrUpdate = true;
					$('#addresourcedialog').dialog('open');
				}
			}, '-', {
				text : '修改',
				iconCls : 'icon-edit',
				handler : function() {
					addOrUpdate = false;
					updateResource();
				}
			}, '-', {
				text : '启用',
				iconCls : 'icon-redo',
				handler : function() {
					updateResourceState(true);
				}
			}, '-', {
				text : '禁用',
				iconCls : 'icon-undo',
				handler : function() {
					updateResourceState(false);
				}
			}, '-', {
				text : '删除',
				iconCls : 'icon-remove',
				handler : function() {
					deleteResource();
				}
			} ],
			columns : [ [ {
				field : 'ck',
				checkbox : true
			}, {
				field : 'parentname',
				title : 'parentname',
				width : 100,
				align : "center",
				hidden : true
			}, {
				field : 'ordernum',
				title : 'ordernum',
				width : 100,
				align : "center",
				hidden : true
			}, {
				field : 'id',
				title : 'ID',
				width : 100,
				align : "center",
				hidden : true
			}, {
				field : 'name',
				title : '资源名称',
				width : 200,
				align : "center"
			}, {
				field : 'url',
				title : 'URL',
				width : 300,
				align : "center"
			}, {
				field : 'type',
				title : '类型',
				width : 100,
				align : "center"
			}, {
				field : 'activite',
				title : '状态',
				width : 200,
				align : "center",
				formatter : function(value, rowData, rowIndex) {
					var str = "<label style='color:red'>禁用</label>";
					if (value) {
						str = "<label style='color:green'>可用</label>";
					}
					return str;
				}
			} ] ]
		});
	}
	//初始化弹出增加批量任务窗口
	function initAddResourceDialog() {
		$('#addresourcedialog').dialog({
			width : 300,
			height : 250,
			title : "增加/修改受控资源",
			closable : true,
			closed : true,
			modal : true,
			buttons : [ {
				text : '确定',
				iconCls : 'icon-ok',
				handler : function() {
					if (addOrUpdate) {
						addResource();
					} else {
						sureUpdateResource();
					}
				}
			}, {
				text : '取消',
				iconCls : 'icon-no',
				handler : function() {
					$('#addresourcedialog').dialog('close');
				}
			} ]
		});
	}
	//增加受控资源
	function addResource() {
		$.ajax({
			url : basePath + "system/addResource",
			data : {
				name : $('#name').val(),
				type : $('#type').val(),
				url : $('#url').val(),
				parentname : $('#parentname').val(),
				ordernum:$('#ordernum').val()
			},
			type : "post",
			dataType : "json",
			success : function(result) {
				if (result.flag) {
					showRightMsg('操作成功');
					$('#resourcetable').treegrid('reload');
				} else {
					showRightMsg('操作失败');
				}
			}
		})
		name: $('#name').val("");
		type: $('#type').val("");
		url: $('#url').val("");
		$('#parentname').val("");
		$('#ordernum').val("");
	}
	//加载子节点
	function loadChildrens(row) {
		var childrens = $('#resourcetable').treegrid('getChildren', row.id);
		for (var i = 0; i < childrens.length; i++) {
			var removeNode = $('#resourcetable').treegrid('find',
					childrens[i].id);
			if (removeNode) {
				$('#resourcetable').treegrid('remove', childrens[i].id);
			}
		}
		$.ajax({
			url : basePath + "system/loadChildrens",
			data : {
				id : row.id
			},
			type : "post",
			dataType : "json",
			async : false,
			success : function(result) {
				if (result.flag) {
					$('#resourcetable').treegrid('append', {
						parent : row.id,
						data : result.rows
					});
					return true;
				} else {
					return false;
				}
			}
		})
	}
	//修改
	function updateResource() {
		var selection = $('#resourcetable').datagrid('getSelected');
		if (selection) {
			$('#addresourcedialog').dialog('open');
			$('#name').val(selection.name);
			$('#type').val(selection.type);
			$('#url').val(selection.url);
			$('#parentname').val(selection.parentname);
			$('#ordernum').val(selection.ordernum);
			$('#id').val(selection.id);
		} else {
			showRightMsg('请选择需要修改的数据');
		}
	}
	//确定保存修改
	function sureUpdateResource() {
		$.ajax({
			url : basePath + "system/updateResource",
			data : {
				id : $('#id').val(),
				name : $('#name').val(),
				type : $('#type').val(),
				url : $('#url').val(),
				ordernum : $('#ordernum').val(),
				parentname : $('#parentname').val()
			},
			type : "post",
			dataType : "json",
			success : function(result) {
				if (result.flag) {
					$('#resourcetable').treegrid('reload');
					showRightMsg('操作成功');
				} else {
					showRightMsg('操作失败');
				}
			}
		})
		name: $('#name').val("");
		type: $('#type').val("");
		url: $('#url').val("");
		$('#parentname').val("");
		$('#ordernum').val("");
	}
	//启用，禁用
	function updateResourceState(activite) {
		var selection = $('#resourcetable').datagrid('getSelected');
		if (selection) {
			$.ajax({
				url : basePath + "system/updateResourceState",
				data : {
					id : selection.id,
					activite : activite
				},
				type : "post",
				dataType : "json",
				async : false,
				success : function(result) {
					if (result.flag) {
						$('#resourcetable').treegrid('reload');
						showRightMsg('操作成功');
					} else {
						showRightMsg('操作失败');
					}
				}
			})
		} else {
			showRightMsg('请选择需要禁用、启用的数据');
		}
	}
	//删除资源
	function deleteResource() {
		var selection = $('#resourcetable').datagrid('getSelected');
		if (selection) {
			$.messager.confirm('确认', '你确定删除该资源吗?', function(r) {
				if (r) {
					$.ajax({
						url : basePath + "system/deleteResource",
						data : {
							id : selection.id
						},
						type : "post",
						dataType : "json",
						success : function(result) {
							if (result.flag) {
								showRightMsg('操作成功');
								$('#resourcetable').treegrid('reload');
							} else {
								showRightMsg('操作失败');
							}
						}
					})
				}
			});
		} else {
			showRightMsg('请选择需要删除的资源');
		}
	}
</script>